/*     */ package com.zimbra.cs.db;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ListUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.DataSource;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Collection;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DbPop3Message
/*     */ {
/*     */   public static final String TABLE_POP3_MESSAGE = "pop3_message";
/*     */   
/*     */   public static void storeUid(Mailbox mbox, String dataSourceId, String uid, int itemId)
/*     */     throws ServiceException
/*     */   {
/*  46 */     if (StringUtil.isNullOrEmpty(uid)) {
/*  47 */       return;
/*     */     }
/*  49 */     DbPool.DbConnection conn = null;
/*  50 */     PreparedStatement stmt = null;
/*     */     try {
/*  52 */       conn = DbPool.getConnection(mbox);
/*     */       
/*  54 */       stmt = conn.prepareStatement("INSERT INTO " + getTableName(mbox) + " (" + DbMailItem.MAILBOX_ID + "data_source_id, uid, item_id) " + "VALUES (" + DbMailItem.MAILBOX_ID_VALUE + "?, ?, ?)");
/*     */       
/*     */ 
/*     */ 
/*  58 */       int pos = 1;
/*  59 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/*  60 */       stmt.setString(pos++, dataSourceId);
/*  61 */       stmt.setString(pos++, uid);
/*  62 */       stmt.setInt(pos++, itemId);
/*  63 */       stmt.executeUpdate();
/*  64 */       conn.commit();
/*     */     } catch (SQLException e) {
/*  66 */       throw ServiceException.FAILURE("Unable to store UID", e);
/*     */     } finally {
/*  68 */       DbPool.closeStatement(stmt);
/*  69 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static void deleteUids(Mailbox mbox, String dataSourceId)
/*     */     throws ServiceException
/*     */   {
/*  78 */     ZimbraLog.mailbox.debug("Deleting UID's for %s", new Object[] { dataSourceId });
/*     */     
/*  80 */     DbPool.DbConnection conn = null;
/*  81 */     PreparedStatement stmt = null;
/*     */     try {
/*  83 */       conn = DbPool.getConnection(mbox);
/*     */       
/*  85 */       stmt = conn.prepareStatement("DELETE FROM " + getTableName(mbox) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "data_source_id = ?");
/*     */       
/*     */ 
/*  88 */       int pos = 1;
/*  89 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/*  90 */       stmt.setString(pos++, dataSourceId);
/*  91 */       int numRows = stmt.executeUpdate();
/*  92 */       conn.commit();
/*  93 */       ZimbraLog.mailbox.debug("Deleted %d UID's", new Object[] { Integer.valueOf(numRows) });
/*     */     } catch (SQLException e) {
/*  95 */       throw ServiceException.FAILURE("Unable to delete UID's", e);
/*     */     } finally {
/*  97 */       DbPool.closeStatement(stmt);
/*  98 */       DbPool.quietClose(conn);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static Map<Integer, String> getMappings(Mailbox mbox, String dataSourceId)
/*     */     throws ServiceException
/*     */   {
/* 107 */     Map<Integer, String> mappings = new HashMap();
/* 108 */     DbPool.DbConnection conn = null;
/* 109 */     PreparedStatement stmt = null;
/* 110 */     ResultSet rs = null;
/*     */     
/* 112 */     ZimbraLog.mailbox.debug("get all POP mappings for %s", new Object[] { dataSourceId });
/*     */     try {
/* 114 */       conn = DbPool.getConnection(mbox);
/* 115 */       stmt = conn.prepareStatement("SELECT item_id, uid FROM " + getTableName(mbox) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + " data_source_id = ?");
/*     */       
/*     */ 
/* 118 */       int pos = 1;
/* 119 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/* 120 */       stmt.setString(pos++, dataSourceId);
/* 121 */       rs = stmt.executeQuery();
/* 122 */       while (rs.next())
/* 123 */         mappings.put(Integer.valueOf(rs.getInt(1)), rs.getString(2));
/*     */     } catch (SQLException e) {
/* 125 */       throw ServiceException.FAILURE("Unable to get UID's", e);
/*     */     } finally {
/* 127 */       DbPool.closeResults(rs);
/* 128 */       DbPool.closeStatement(stmt);
/* 129 */       DbPool.quietClose(conn);
/*     */     }
/* 131 */     ZimbraLog.mailbox.debug("Found %d POP mappings for %s", new Object[] { Integer.valueOf(mappings.size()), dataSourceId });
/*     */     
/* 133 */     return mappings;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Set<String> getMatchingUids(Mailbox mbox, DataSource ds, Collection<String> uids)
/*     */     throws ServiceException
/*     */   {
/* 143 */     ZimbraLog.mailbox.debug("%s: looking for uids that match a set of size %d", new Object[] { ds, Integer.valueOf(uids.size()) });
/*     */     
/* 145 */     List<List<String>> splitIds = ListUtil.split(uids, Db.getINClauseBatchSize());
/* 146 */     Set<String> matchingUids = new HashSet();
/*     */     
/* 148 */     DbPool.DbConnection conn = null;
/* 149 */     PreparedStatement stmt = null;
/* 150 */     ResultSet rs = null;
/*     */     try {
/* 152 */       conn = DbPool.getConnection(mbox);
/*     */       
/* 154 */       for (Iterator i$ = splitIds.iterator(); i$.hasNext(); 
/*     */           
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 169 */           stmt = null)
/*     */       {
/* 154 */         List<String> curIds = (List)i$.next();
/* 155 */         stmt = conn.prepareStatement("SELECT uid FROM " + getTableName(mbox) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "data_source_id = ?" + " AND " + DbUtil.whereIn("uid", curIds.size()));
/*     */         
/*     */ 
/*     */ 
/* 159 */         int pos = 1;
/* 160 */         pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/* 161 */         stmt.setString(pos++, ds.getId());
/* 162 */         for (String uid : curIds)
/* 163 */           stmt.setString(pos++, uid);
/* 164 */         rs = stmt.executeQuery();
/*     */         
/* 166 */         while (rs.next())
/* 167 */           matchingUids.add(rs.getString(1));
/* 168 */         rs.close();rs = null;
/* 169 */         stmt.close();
/*     */       }
/*     */     } catch (SQLException e) {
/* 172 */       throw ServiceException.FAILURE("Unable to get UID's", e);
/*     */     } finally {
/* 174 */       DbPool.closeResults(rs);
/* 175 */       DbPool.closeStatement(stmt);
/* 176 */       DbPool.quietClose(conn);
/*     */     }
/*     */     
/* 179 */     ZimbraLog.mailbox.debug("Found %d matching UID's", new Object[] { Integer.valueOf(matchingUids.size()) });
/* 180 */     return matchingUids;
/*     */   }
/*     */   
/*     */   public static String getTableName(int mailboxId, int groupId) {
/* 184 */     return DbMailbox.qualifyTableName(groupId, "pop3_message");
/*     */   }
/*     */   
/*     */   public static String getTableName(Mailbox mbox) {
/* 188 */     return DbMailbox.qualifyTableName(mbox, "pop3_message");
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/DbPop3Message.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */